﻿// ## 2023/09/27 # 氕氘氚 # 重构 ##

namespace TRAP_DAMAGE
{
	class Main
	{

		Main()
		{
			pk::set_func(213, pk::func213_t(callback));
		}

		void callback(pk::damage_info& info, int trap, pk::unit@ attacker, pk::hex_object@ target, bool critical)
		{
			int n = 0;

			if (target.is_instance(pk::unit::type_id))  //部队
			{
				pk::unit@ target_unit = pk::hex_object_to_unit(target);

				switch (trap)
					{
					case 设施_火种:
						n = 部队火伤_火种;
						break;
					case 设施_火球:
						n = 部队火伤_火球;
						break;
					case 设施_火船:
						n = 部队火伤_火船;
						break;
					case 设施_火焰种:
						n = 部队火伤_火焰种;
						break;
					case 设施_火焰球:
						n = 部队火伤_火焰球;
						break;
					case 设施_业火种:
						n = 部队火伤_业火种;
						break;
					case 设施_业火球:
						n = 部队火伤_业火球;
						break;
					case 设施_落石:
						n = 部队落石伤害;
						break;
					default:
						n = 0;
						break;
				}
				n = int(n * ch::randfloat(0.8, 1.2));

				if (trap == 设施_落石)  //落石
				{
					n = int(batt::unit_def_impacts(float(n), target_unit));
				}
				else  //火陷阱
				{
					if (pk::is_alive(attacker))
					{
						if (ch::has_skill(attacker, 特技_火神))
							info.atk_skill = 特技_火神;
						else if (ch::has_skill(attacker, 特技_火攻))
							info.atk_skill = 特技_火攻;
					}
					if (ch::has_skill(target_unit, 特技_藤甲))
						info.def_skill = 特技_藤甲;
					else if (ch::has_skill(target_unit, 特技_火神) or ch::has_skill(target_unit, 特技_辟火))
						info.def_skill = 特技_火神;

					n = batt::fire_skill_impacts(n, attacker, target_unit);
				}
				if (ch::has_skill(target_unit, 特技_踏破))
				{
					n = int(n * pk::core::skill_constant_value(特技_踏破) / 100.f);
					info.def_skill = 特技_踏破;
				}
				
				info.troops_damage = n;
			}
			else //建筑
			{
				pk::building@ building = pk::hex_object_to_building(target);

				//提防未恢复不能造成伤害
				if (trap == 设施_堤防)
				{
					if (!building.completed)
					{
						info.hp_damage = 0;
						return;
					}
					else
					{
						n = pk::hex_object_to_building(target).hp;
						info.hp_damage = pk::max(n - pk::max(n - pk::rand(40) - 980, 1), 0);
						return;
					}
				}
				else
				{
					switch (trap)
					{
						case 设施_火种:
							n = 建筑火伤_火种;
							break;
						case 设施_火球:
							n = 建筑火伤_火球;
							break;
						case 设施_火船:
							n = 建筑火伤_火船;
							break;
						case 设施_火焰种:
							n = 建筑火伤_火焰种;
							break;
						case 设施_火焰球:
							n = 建筑火伤_火焰球;
							break;
						case 设施_业火种:
							n = 建筑火伤_业火种;
							break;
						case 设施_业火球:
							n = 建筑火伤_业火球;
							break;
						case 设施_落石:
							n = 建筑落石伤害;
							break;
						default:
							n = 0;
							break;
					}
					n = int(n * ch::randfloat(0.8, 1.2));

					
					if (trap != 设施_落石)  //火陷阱
					{
						if (pk::is_alive(attacker))
						{
							if (ch::has_skill(attacker, 特技_火神))
								info.atk_skill = 特技_火神;
							else if (ch::has_skill(attacker, 特技_火攻))
								info.atk_skill = 特技_火攻;
						}

						n = batt::fire_atk_skill_impact(n, attacker);
					}
					
					info.hp_damage = n;
				}
			}

			//pk::trace(pk::format("213 TRAP_DAMAGE {} {}", info.troops_damage, info.hp_damage));
		}
	}

	Main main;
}